package com.xsd.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Description: FileChannel案例1
 * @Author: zhoufuzong
 * @Created: 2021-01-21 22:06:23
 * @Version: 1.1.0
 */

public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str ="hello,java";

        // 创建一个输出流-> channel
        FileOutputStream fileOutputStream = new FileOutputStream("file01.txt");

        // 通过fileOutputStream 获取对应的FileChannel
        // 通过这个fileChannel 真实类型是FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将str 放入到缓冲区
        byteBuffer.put(str.getBytes());
        // 对byteBuffer 进行flip
        byteBuffer.flip();

        // 将byteBuffer 数据写入fileChannel
        fileChannel.write(byteBuffer);
        // 关闭流
        fileOutputStream.close();

    }
}
